<?php
Yii::import('application.components.Reference');
class DosController extends GxController
{
    public function actionCreate()
    {
        $model = new Dos;
        if (!Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            $status = false;
            $msg = 'DO Sementara berhasil dibuat.';
            Yii::import('application.components.U');
            app()->db->autoCommit = false;
            $transaction = Yii::app()->db->beginTransaction();
            try {
                $dos = new Dos;
                $docref = $dos->doc_ref;
                $ref = new Reference();
                $docref = $ref->get_next_reference(DOS);
                $dos->salesman_id = $_POST['sales'];
                $dos->tgl = $_POST['tgl'];
                $dos->doc_ref = $docref;
                if (!$dos->save())
                    throw new Exception("Gagal menyimpan DO Sementara. ".CHtml::errorSummary($dos));
                $id = $dos->dos_id;
                Yii::app()->db->createCommand("INSERT INTO {{dos_detil}} (dos_id,barang_id,jml,sat,pcs)
                SELECT $id,pdp.barang_id, SUM(pdp.jml), pdp.sat, SUM(pdp.pcs)
                FROM {{detil_penjualan}} pdp
                INNER JOIN {{penjualan}} pp ON ( pdp.penjualan_id = pp.penjualan_id  )
                WHERE pp.tgl = :tgl AND pp.salesman_id = :salesman_id AND pp.bruto >= 0
                GROUP BY pdp.barang_id, pdp.sat")->execute(array(':salesman_id' => $_POST['sales'],
                        ':tgl' => $_POST['tgl']));
                $ref->save(DOS, $id, $docref);
                $criteria = new CDbCriteria();
                $criteria->addCondition("dos_id = :dos_id");
                $criteria->params = array(':dos_id' => $id);
                /* @var $dosdetils DosDetil[] */
                $dosdetils = DosDetil::model()->findAll($criteria);
                foreach($dosdetils as $dosdetil){
                    U::add_stock_moves(DOS, $id, $dos->tgl,
                        $dosdetil->barang_id, GUDANG_PST, -$dosdetil->pcs, $dos->doc_ref,
                        0);
                    U::add_stock_moves(DOS, $id, $dos->tgl,
                        $dosdetil->barang_id, GUDANG_SMT, $dosdetil->pcs, $dos->doc_ref,
                        0);
                }
                $transaction->commit();
                $status = true;
            } catch (Exception $ex) {
                $transaction->rollback();
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'id' => $docref,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionIndex()
    {
        $criteria = new CDbCriteria();
        if ((isset($_POST['sales']) && isset($_POST['tgl']))) {
            $criteria->addCondition("salesman_id = :salesman_id");
            $criteria->addCondition("tgl = :tgl");
            $criteria->params = array(':salesman_id' => $_POST['sales'], ':tgl' => $_POST['tgl']);
            $model = Dos::model()->findAll($criteria);
            $total = Dos::model()->count($criteria);
            $this->renderJson($model, $total);
        }
    }
}